//
//  NSError+MKAPIHttpRequest.h
//  MKAPI
//
//  Created by 苏尚进 on 2022/11/5.
//  Copyright © 2022 moon. All rights reserved.
//

#import <Foundation/Foundation.h>

enum : NSUInteger {
    MKAPIRequestErrorServiceReturnError=-1011500,   //服务器请求返回错误信息
    MKAPIRequestErrorNetworkFrameworkError=-1011501,//网络框架返回了错误信息
    MKAPIRequestErrorInvalidateError=-1011502,//请求前置检验失败
};

NS_ASSUME_NONNULL_BEGIN

extern NSErrorDomain const MKAPIRequestInvalidateErrorDomain;
extern NSErrorUserInfoKey const MKAPIRequestErrorUrlKey;//=url
extern NSErrorUserInfoKey const MKAPIRequestErrorServiceCodeKey;//=code
extern NSErrorUserInfoKey const MKAPIRequestErrorServiceDescKey;//=desc

@interface NSError (MKAPIHttpRequest)
@property(nonatomic,readonly,nullable) NSString *mkapi_url;//http请求的url值
@property(nonatomic,readonly,nullable) NSString *mkapi_serviceCode;//http请求的服务器错误码
@property(nonatomic,readonly,nullable) NSString *mkapi_serviceDesc;//http请求的服务器错误信息

@property(nonatomic,readonly,nullable) NSString *mkapi_displayCode;//用于UI展示错误信息的错误码，展示顺序为mkapi_serviceCode，code
@property(nonatomic,readonly,nullable) NSString *mkapi_displayDesc;//用于UI展示错误信息的描述，展示顺序为mkapi_serviceDesc,NSLocalizedDescriptionKey,NSLocalizedFailureReasonErrorKey

extern NSErrorUserInfoKey const MKAPIRequestErrorServiceDescKey;//=desc
/// 构造请求失败对象。
/// - Parameters:
///   - url: 请求的url值，将会放到error.userInfo.url值
///   - code: error的错误码，如MKAPIRequestErrorServiceReturnError，MKAPIRequestErrorNetworkFrameworkError
///   - serviceCode: http请求的服务器错误码
///   - serviceDesc: http请求的服务器错误信息
///   - userInfo: 错误扩展信息
+ (NSError *)mkapi_errorWithUrl:(nullable NSString *)url code:(NSInteger)code serviceCode:(nullable NSString *)serviceCode serviceDesc:(nullable NSString *)serviceDesc userInfo:(nullable NSDictionary *)userInfo;

/// 构造前置检查失败的错误对象，error.domain=MKAPIRequestInvalidateErrorDomain,error.code=MKAPIRequestErrorInvalidateError
/// - Parameters:
///   - serviceCode: 前置检查不通过的错误码
///   - serviceDesc: 前置检查不通过的错误文字信息
///   - userInfo: 错误扩展信息
+ (NSError *)mkapi_invalidateErrorWithServiceCode:(nullable NSString *)serviceCode serviceDesc:(nullable NSString *)serviceDesc userInfo:(nullable NSDictionary *)userInfo;

@property(nonatomic,readonly) BOOL mkapi_isCertValidError;//是否为证书检验失败的错误
+ (nonnull NSError *)mkapi_certValidErrorForServerTrust:(nullable SecTrustRef)serverTrust url:(nullable NSURL *)url;
@end

NS_ASSUME_NONNULL_END
